package com.hlj.ncp.warehouse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.hlj.ncp.common.core.domain.AjaxResult;
import com.hlj.ncp.warehouse.entity.ColdStorageMonitor;
import com.hlj.ncp.warehouse.service.IColdStorageMonitorService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 冷库湿温度监控 控制器
 */
@Controller
@RequestMapping("/warehouse/coldStorageMonitor")
public class ColdStorageMonitorController {
    
    @Autowired
    private IColdStorageMonitorService coldStorageMonitorService;

    /**
     * 冷库湿温度监控页面
     */
    @GetMapping("/index")
    public String index() {
        return "warehouse/coldStorageMonitor/index";
    }
    
    /**
     * 冷库温湿度历史数据页面
     */
    @GetMapping("/history")
    public String history() {
        return "warehouse/coldStorageMonitor/history";
    }
    
    /**
     * 冷库温湿度趋势分析页面
     */
    @GetMapping("/trend")
    public String trend() {
        return "warehouse/coldStorageMonitor/trend";
    }

    /**
     * 查询冷库湿温度监控列表
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(ColdStorageMonitor coldStorageMonitor) {
        List<ColdStorageMonitor> list = coldStorageMonitorService.selectColdStorageMonitorList(coldStorageMonitor);
        return AjaxResult.success(list);
    }

    /**
     * 获取冷库湿温度监控详细信息
     */
    @GetMapping(value = "/{monitorId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("monitorId") Long monitorId) {
        return AjaxResult.success(coldStorageMonitorService.selectColdStorageMonitorById(monitorId));
    }

    /**
     * 新增冷库湿温度监控
     */
    @PostMapping
    @ResponseBody
    public AjaxResult add(@RequestBody ColdStorageMonitor coldStorageMonitor) {
        return AjaxResult.success(coldStorageMonitorService.insertColdStorageMonitor(coldStorageMonitor));
    }

    /**
     * 修改冷库湿温度监控
     */
    @PutMapping
    @ResponseBody
    public AjaxResult edit(@RequestBody ColdStorageMonitor coldStorageMonitor) {
        return AjaxResult.success(coldStorageMonitorService.updateColdStorageMonitor(coldStorageMonitor));
    }

    /**
     * 删除冷库湿温度监控
     */
    @DeleteMapping("/{monitorIds}")
    @ResponseBody
    public AjaxResult remove(@PathVariable Long[] monitorIds) {
        return AjaxResult.success(coldStorageMonitorService.deleteColdStorageMonitorByIds(monitorIds));
    }
    
    /**
     * 处理冷库湿温度告警
     */
    @PutMapping("/process")
    @ResponseBody
    public AjaxResult processAlarm(@RequestBody ColdStorageMonitor coldStorageMonitor) {
        return AjaxResult.success(coldStorageMonitorService.processColdStorageMonitorAlarm(coldStorageMonitor));
    }
    
    /**
     * 获取最新的监控数据
     */
    @GetMapping("/latest/{warehouseId}")
    @ResponseBody
    public AjaxResult getLatestData(@PathVariable("warehouseId") Long warehouseId) {
        List<ColdStorageMonitor> list = coldStorageMonitorService.selectLatestMonitorData(warehouseId);
        return AjaxResult.success(list);
    }
    
    /**
     * 获取监控统计数据
     */
    @GetMapping("/stats/{warehouseId}")
    @ResponseBody
    public AjaxResult getMonitorStats(@PathVariable("warehouseId") Long warehouseId) {
        List<Map<String, Object>> stats = coldStorageMonitorService.selectMonitorStats(warehouseId);
        return AjaxResult.success(stats);
    }
    
    /**
     * 查询历史监控数据
     */
    @GetMapping("/history/data")
    @ResponseBody
    public AjaxResult getHistoryData(
            @RequestParam("warehouseId") Long warehouseId,
            @RequestParam(value = "storageArea", required = false) String storageArea,
            @RequestParam(value = "deviceId", required = false) String deviceId,
            @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        List<ColdStorageMonitor> list = coldStorageMonitorService.selectHistoryData(
                warehouseId, storageArea, deviceId, startTime, endTime);
        return AjaxResult.success(list);
    }
    
    /**
     * 查询按小时聚合的趋势数据
     */
    @GetMapping("/trend/hour")
    @ResponseBody
    public AjaxResult getTrendDataByHour(
            @RequestParam("warehouseId") Long warehouseId,
            @RequestParam(value = "storageArea", required = false) String storageArea,
            @RequestParam(value = "deviceId", required = false) String deviceId,
            @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        List<Map<String, Object>> list = coldStorageMonitorService.selectTrendDataByHour(
                warehouseId, storageArea, deviceId, startTime, endTime);
        return AjaxResult.success(list);
    }
    
    /**
     * 查询按天聚合的趋势数据
     */
    @GetMapping("/trend/day")
    @ResponseBody
    public AjaxResult getTrendDataByDay(
            @RequestParam("warehouseId") Long warehouseId,
            @RequestParam(value = "storageArea", required = false) String storageArea,
            @RequestParam(value = "deviceId", required = false) String deviceId,
            @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        List<Map<String, Object>> list = coldStorageMonitorService.selectTrendDataByDay(
                warehouseId, storageArea, deviceId, startTime, endTime);
        return AjaxResult.success(list);
    }
}